﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

public partial class _Default : System.Web.UI.Page//, ICallbackEventHandler
{
    public string result;

    protected void Page_Load(object sender, EventArgs e)
    {
        RenderJSArrayWithCliendIds(txtHidDataX);
        RenderJSArrayWithCliendIds(txtHidDataY);
        (this.Master as SiteMaster).LogoutClicked += new EventHandler(child_LogoutClicked);

        CheckLoginUser();
  
        if (!Page.IsPostBack)
        {
            try
            {
                TypesManager.FillDDL(ddlType);

                PlacesManager placesMan = new PlacesManager(1);
                StringBuilder sb = new StringBuilder();
                sb.Append("<script>");
                sb.Append("var arrPlace = new Array;");
                sb.Append("var myMarkers = [];");
                int i = 0;
                while (placesMan.Next())
                {
                    Place p = placesMan.GetCurrent();
                    sb.Append("var t =  new Object();");
                    sb.Append("t.id = " + p.PlaceID + ";");
                    sb.Append("t.lat = " + p.PlaceLat + ";");
                    sb.Append("t.lng = " + p.PlaceLng + ";");
                    sb.Append("t.title = '" + p.PlaceTitle + "';");
                    sb.Append("t.typeid = " + p.PlaceType + ";");
                    sb.Append("arrPlace[" + (i++) + "] = t;");
                }
                sb.Append("</script>");
                ClientScript.RegisterStartupScript(this.GetType(), "TestArrayScript", sb.ToString());

                if (!Master.Page.ClientScript.IsStartupScriptRegistered("addAll"))
                {
                    Master.Page.ClientScript.RegisterStartupScript(this.GetType(), "addAll", "javascript:addAllPlaces();", true);
                }

                if (!Master.Page.ClientScript.IsStartupScriptRegistered("changePlaceType"))
                {
                    Master.Page.ClientScript.RegisterStartupScript(this.GetType(), "changePlaceType", "javascript:changePlaceType(0);", true);
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }
    }

    public void RenderJSArrayWithCliendIds(params Control[] wc)
    {
        if (wc.Length > 0)
        {
            StringBuilder arrClientIDValue = new StringBuilder();
            StringBuilder arrServerIDValue = new StringBuilder();

            // Get a ClientScriptManager reference from the Page class.
            ClientScriptManager cs = Page.ClientScript;

            // Now loop through the controls and build the client and server id's
            for (int i = 0; i < wc.Length; i++)
            {
                arrClientIDValue.Append("\"" + wc[i].ClientID + "\",");
                arrServerIDValue.Append("\"" + wc[i].ID + "\",");
            }
            // Register the array of client and server id to the client
            cs.RegisterArrayDeclaration("MyClientID", arrClientIDValue.ToString().Remove(arrClientIDValue.ToString().Length - 1, 1));
            cs.RegisterArrayDeclaration("MyServerID", arrServerIDValue.ToString().Remove(arrServerIDValue.ToString().Length - 1, 1));
            // Now register the method GetClientId, used to get the client id of tthe control
            cs.RegisterStartupScript(this.Page.GetType(), "key", "\nfunction GetClientId(serverId)\n{\nfor(i=0; i<MyServerID.length; i++)\n{\nif ( MyServerID[i] == serverId )\n{\nreturn MyClientID[i];\nbreak;\n}\n}\n}", true);
        }
    }

    void child_LogoutClicked(object sender, EventArgs e)
    {
        Session["User"] = null;
        //ibtnLogout.Visible = false;
        //HeadLoginName.Visible = false;
        //HeadLoginStatus.Visible = true;
    }

    private void CheckLoginUser()
    {
        ImageButton logoutImageButton = (ImageButton)Master.FindControl("ibtnLogout");
        if (logoutImageButton != null)
            logoutImageButton.Visible = false;

        //if(Request.QueryString["logout"]!=null)
        //{
        //    string sCheckLogout = Request.QueryString["logout"].ToString();
        //    if (sCheckLogout.Equals("1"))
        //    {
        //        Session["User"] = null;
        //        if (cLoginName != null)
        //        {
        //            cLoginName.Visible = true;
        //        }
        //    }
        //}
        //else
        if (Session["User"] != null)
        {
            Label cLoginName = (Label)Master.FindControl("HeadLoginName");
            if (cLoginName != null)
            {
                string sUserName = Session["User"].ToString();
                cLoginName.Text = "Welcome " + sUserName;

                LinkButton lnkLogin = (LinkButton)Master.FindControl("HeadLoginStatus");
                lnkLogin.Visible = false;

                if (logoutImageButton != null) logoutImageButton.Visible = true;
            }
        }
    }

    protected void btnTrackThePosition_Click(object sender, EventArgs e)
    {
        try
        {
            //Response.Write(txtHidDataX.Value);
            //Response.Write(txtHidDataY.Value);
            double dLat = Convert.ToDouble(txtHidDataX.Value);
            double dLng = Convert.ToDouble(txtHidDataY.Value);

            MapControl.TrackPoint(txtTitle.Text, txtDescription.Text, "", dLat, dLng, 1);
            txtTitle.Text = "";
            txtDescription.Text = "";
            txtHidDataX.Value = "";
            txtHidDataY.Value = "";
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }

    protected void Timer1_Tick(object sender, EventArgs e)
    {
       // lblNow.Text = DateTime.Now.ToString("hh:mm:ss");
    }



    //protected void Timer1_Tick(object sender, EventArgs e)
    //{
    //    //UpdatePanel1.Update();
    //    //btnTrackThePosition_Click(null, null);
    //    DateStampLabel.Text = DateTime.Now.ToString();
       
    //}

    //#region ICallbackEventHandler Members

    //private string returnValue;
    //public string GetCallbackResult()
    //{
    //    return returnValue;
    //}

    //public void RaiseCallbackEvent(string eventArgument)
    //{
    //    returnValue = DateTime.Now.ToString();
    //}

    //#endregion
}
